﻿using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Common;
using log4net;
using SqlSugar;
using AutoFacUtils;
using CashFlow.Web.Def;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using CashFlow.Db.Repository;
using CashFlow.Web.Service;

namespace CashFlow.Web.Service.Implement
{
    public class AppointmentService : MyService<AppointmentEntity>, IAppointmentService
    {
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }

        public EAppointmentResult DoAppointment(int userId, string type)
        {
            EAppointmentResult result = EAppointmentResult.Fail;

            string date = DateTime.Now.ToString("yyyy-MM-dd");
            using (var db = SqlSugarHelper.GetDb())
            {
                int exist = db.Queryable<AppointmentEntity>()
                              .Where(x => x.UserId == userId
                                    && x.Time.ToString("yyyy-MM-dd") == date)
                              .Count();
                if (exist == 0)
                {
                    var entity = new AppointmentEntity
                    {
                        Time = DateTime.Now,
                        Type = type,
                        UserId = userId
                    };
                    int n = db.Insertable(entity).ExecuteCommand();
                    if (n > 0)
                    {
                        result = EAppointmentResult.Success;
                    }
                }
                else
                {
                    result = EAppointmentResult.Repeat;
                }
            }

            return result;
        }

        public IList<UserInfoEntity> QueryTeamNotAppointmentUsers(
            int userId
            , string date)
        {
            var service = GetUserService();
            var ls = service.QueryShopUsersByUserId(userId, 1);
            var userIds = ls.Select(x => x.Id).ToList();
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls2 = db.Queryable<AppointmentEntity>()
                            .Where(x => x.Time.ToString("yyyy-MM-dd") == date
                                && userIds.Contains(x.UserId))
                            .ToList();
                var ls3 = ls.Where(x => !ls2.Exists(y => y.UserId == x.Id)).ToList();
                return ls3;
            }
        }

        public IList<UserInfoEntity> QueryTeamAppointmentUsers(
            int userId
            , string date)
        {
            var service = GetUserService();
            var ls = service.QueryShopUsersByUserId(userId, 1);
            var userIds = ls.Select(x => x.Id).ToList();
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls2 = db.Queryable<AppointmentEntity>()
                            .Where(x => x.Time.ToString("yyyy-MM-dd") == date
                                && userIds.Contains(x.UserId))
                            .ToList();
                var ls3 = ls.Where(x => ls2.Exists(y => y.UserId == x.Id)).ToList();
                return ls3;
            }
        }

        public IList<AppointmentUserInfoView2> QueryAllByUserId(int userId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                var ls = db.Queryable<AppointmentEntity>()
                           .InnerJoin<UserInfoEntity>((a, u) => a.UserId == u.Id)
                           .Where((a, u) => u.Id == userId)
                           .Select((a, u) => new AppointmentUserInfoView2
                           {
                               AppointmentInfo = a,
                               UserInfo = u
                           })
                           .ToList();
                return ls;
            }
        }
    }
}
